var recommand = new vo.newObj({
	el:'#recommand',
	datas:{},
	methods:{
		load:function(){
			ajax({
				method:'GET',
				url:'article/recommand/'+blog.id,
				success:function(response){
					var datas = response.datas;
					if(typeof(datas) == 'undefined'|| datas.length == 0){
						var content = "<div class='info' >"+
								"<span>博主很懒，什么都没写</span>"+
								"</div>";
						recommand.setDatas({loading:'',
							content:content});
						recommand.flush();
					}else{
						recommand.setDatas({loading:''});
						recommand.flush();
						for(var index in datas){
							var data = datas[index];
							//工厂方法构建文章对象（用vo-js）
							article(data,recommand.el.id);
						}
					}
				},
				error:function(){
					var message = "数据加载失败";
					throw new Error(message);
					recommand.setDatas({loading:message});
					recommand.flush();
				}
			});
		}
	}
});
/**文章对象工厂
 * 8/24 用createElement效率太低（极差）
 * 8/25 用innerHTML提高效率(差）
 * 8/25 事实证明正则才是王道！
 * @param data
 * @returns
 */
var article = function(){//用闭包来实现工厂模式，闭包内保存静态的模板和方法
	var article = "<div class='index-blog add-line'>"+
		"<span class='icon opacity btn-border'>${type}</span>"+
		"<div class='title-content'><a href='${bpath}/article/${id}' class='title'>${title}</a><i vo-m='click:spread' class='spread'></i></div>"+
		"<div id='hidden${id}' class='hidden'><div class='description'>${description}</div></div>"+
		"<div class='tags'><a class='read'><i class='icon-read'></i>"+
		"阅读量(${readers)</a>"+
		"<a class='comment'><i class='icon-comment'></i>"+
		"评论量(${comments})</a>"+
		"</div></div>";
	function spread(datas){
		var obj = getElementsByAttr_r(this.el,'id','hidden\\w+')[0];
		var speed = datas.speed;
		var height = datas.height;
		var isSpread = datas.isSpread;
		if(!isSpread){
			this.modify('isSpread',true);
  			obj.disabled = true;
  			if(height <= 0){
	  			obj.style.position = "absolute";
				obj.style.visibility = "hidden";
				obj.style.display = "block";
				height = obj.offsetHeight;
				this.modify('height',height);
				obj.style.height = 0+"px";
				obj.style.visibility = "";
				obj.style.position = "";
  			}
  			console.log(height);
			obj.timer = setInterval(function(){
				obj.style.height = obj.offsetHeight + speed +"px";
				if(obj.offsetHeight >= height){
					clearInterval(obj.timer);
				}
			});
  		}else{
  			this.modify('isSpread',false);
  			obj.disabled = "false";
  			obj.timer = setInterval(function(){
  				obj.style.height = obj.offsetHeight - speed +"px";
  				if(obj.offsetHeight <= 0){
  					clearInterval(obj.timer);
  				}
  			});
  		}
	};
	function creatArticle(data,id){
		var copy = article;
		var reg = new RegExp("\\${(\\w+)}?","g");
		var result = copy.replace(reg,function($0,$1){
			if($1 == "type"){
				if(data[$1] == "0"){
					return "原";
				}else if(data[$1] == "1"){
					return "转";
				}else if(data[$1] == "2"){
					return "藏";
				}
			}else if($1 == "bpath"){
				return blog.path;
			}else{
				return data[$1];
			}
		});
		var newObj = new vo.newObj({
			el:'#'+data['id'],
			parent:'#'+id,
			html:result,
			datas:{
				isSpread:false,//控制子元素的收缩展开
				speed :1.7,//伸张速度
				height : 0//子 元素高度
			},
			methods:{
				spread:spread
			}
		});
		return newObj;
	}
	return creatArticle;
}();
recommand.methods.load();